//
//  UIViewController+ZWPopover.m
//  Ymd
//
//  Created by 云免 on 2018/12/22.
//  Copyright © 2018年 云免. All rights reserved.
//

#import "UIViewController+ZWPopover.h"
#import <objc/runtime.h>
#import "ZWPopoverAnimator.h"

static const NSString *kPopoverAnimatorKey = @"kPopoverAnimatorKey";

@implementation UIViewController (ZWPopover)

- (ZWPopoverAnimator *)popoverAnimator {
    return objc_getAssociatedObject(self, &kPopoverAnimatorKey);
}

- (void)setPopoverAnimator:(ZWPopoverAnimator *)popoverAnimator {
    objc_setAssociatedObject(self, &kPopoverAnimatorKey, popoverAnimator, OBJC_ASSOCIATION_RETAIN);
}

- (void)zw_centerPresentController:(UIViewController *)viewController presentedSize:(CGSize)size completeHandle:(ZWCompleteHandle)completeHandle {
    dispatch_async(dispatch_get_main_queue(), ^{
        self.popoverAnimator = [ZWPopoverAnimator popoverAnimatorWithStyle:ZWPopoverTypeAlert completeHandle:completeHandle];
        [self.popoverAnimator setCenterViewSize:size];
        viewController.modalPresentationStyle = UIModalPresentationCustom;
        viewController.transitioningDelegate = self.popoverAnimator;
        [self presentViewController:viewController animated:YES completion:nil];
    });
}

- (void)zw_bottomPresentController:(UIViewController *)vc presentedHeight:(CGFloat)height completeHandle:(ZWCompleteHandle)completion{
    
    dispatch_async(dispatch_get_main_queue(), ^{
        self.popoverAnimator = [ZWPopoverAnimator popoverAnimatorWithStyle:ZWPopoverTypeActionSheet completeHandle:completion];
        [self.popoverAnimator setBottomViewHeight:height];
        vc.modalPresentationStyle = UIModalPresentationCustom;
        vc.transitioningDelegate = self.popoverAnimator;
        [self presentViewController:vc animated:YES completion:nil];
    });
}

- (void)zw_showAlertViewController:(UIViewController *)viewController {
    dispatch_async(dispatch_get_main_queue(), ^{
        self.popoverAnimator = [ZWPopoverAnimator popoverAnimatorWithStyle:ZWPopoverTypeAlert];
        [self.popoverAnimator setCenterViewSize:CGSizeMake(280, 120)];
        viewController.modalPresentationStyle = UIModalPresentationCustom;
        viewController.transitioningDelegate = self.popoverAnimator;
        [self presentViewController:viewController animated:YES completion:nil];
    });
}

- (void)zw_showOrderAlertViewController:(UIViewController *)viewController {
    dispatch_async(dispatch_get_main_queue(), ^{
        self.popoverAnimator = [ZWPopoverAnimator popoverAnimatorWithStyle:ZWPopoverTypeAlert];
        [self.popoverAnimator setCenterViewSize:CGSizeMake(280, 151)];
        viewController.modalPresentationStyle = UIModalPresentationCustom;
        viewController.transitioningDelegate = self.popoverAnimator;
        [self presentViewController:viewController animated:YES completion:nil];
    });
}

- (void)zw_showActivityAlertViewController:(UIViewController *)viewController {
    dispatch_async(dispatch_get_main_queue(), ^{
        self.popoverAnimator = [ZWPopoverAnimator popoverAnimatorWithStyle:ZWPopoverTypeAlert];
        [self.popoverAnimator setCenterViewSize:CGSizeMake(280, 150)];
        viewController.modalPresentationStyle = UIModalPresentationCustom;
        viewController.transitioningDelegate = self.popoverAnimator;
        [self presentViewController:viewController animated:YES completion:nil];
    });
}

- (void)zw_centerPresentVersionController:(UIViewController *)viewController {
    dispatch_async(dispatch_get_main_queue(), ^{
        self.popoverAnimator = [ZWPopoverAnimator popoverAnimatorWithStyle:ZWPopoverTypeActionAlertDotTouch];
        [self.popoverAnimator setCenterViewSize:CGSizeMake(280, 296)];
        viewController.modalPresentationStyle = UIModalPresentationCustom;
        viewController.transitioningDelegate = self.popoverAnimator;
        [self presentViewController:viewController animated:YES completion:nil];
    });
}

@end
